import utils from '@/lib/utils';
export default class TreeTool {
    private idKey: string = "";
    private pidKey: string = "";
    constructor(idKey: string = "id", pidKey: string = "pid") {
        this.idKey = idKey;
        this.pidKey = pidKey;
    }
    /**
     * 列表转树
     * @param list 数据列表
     * @param id 子项的父ID
     * @param level 级别
     */
    public listToTree(list: any[], id: string = "", level = 1): any[] {
        if (utils.empty(list)) {
            return [];
        }
        const resArr: any[] = [];
        let rowIndex: number = 1;
        list.forEach((item: any, key: number) => {
            if (item[this.pidKey] === id) {
                const temItem = utils.CopyNewObject(item);
                temItem.level = level;
                temItem.childrenIndex = rowIndex;
                temItem.children = this.listToTree(list, temItem[this.idKey], level + 1);
                resArr.push(temItem);
                rowIndex++;
            }
        });
        return resArr;
    }
    /**
     * 获取树结构最大有多少级
     */
    public getTreeMaxLevel(list: any[], level = 0): number {
        if (utils.empty(list)) {
            return 0;
        }
        let maxLevel = level;
        for (const item of list) {
            if (item.level > maxLevel) {
                maxLevel = item.level;
            }
            if (!utils.empty(item.children) && item.children.length > 0) {
                const temLevel = this.getTreeMaxLevel(item.children, maxLevel);
                if (temLevel > maxLevel) {
                    maxLevel = temLevel;
                }
            }
        }
        return maxLevel;
    }
    /**
     * 添加数据到树中
     */
    public add(list: any[], data: any) {
        if (utils.empty(list)) {
            return;
        }
        if (utils.empty(data[this.pidKey])) {
            list.push(data);
        } else {
            for (const item of list) {
                if (item[this.idKey] === data[this.pidKey]) {
                    if (utils.empty(item.children)) {
                        item.children = [data];
                    } else {
                        item.children.push(data);
                    }
                    break;
                } else {
                    if (!utils.empty(item.children) && item.children.length > 0) {
                        this.add(item.children, data);
                    }
                }
            }
        }
    }
    /**
     * 删除多条
     */
    public deletes(list: any[], ids: string[]) {
        if (utils.empty(list)) {
            return;
        }
        for (const id of ids) {
            this.delete(list, id);
        }
    }
    /**
     * 删除一条
     */
    public delete(list: any[], id: string) {
        if (utils.empty(list)) {
            return;
        }
        list.forEach((item: any, index: number) => {
            if (item[this.idKey] === id) {
                list.splice(index, 1);
                return;
            } else {
                if (!utils.empty(item.children) && item.children.length > 0) {
                    this.delete(item.children, id);
                }
            }
        });
    }
    /**
     * 修改数据
     */
    public edit(list: any[], data: any) {
        if (utils.empty(list)) {
            return;
        }
        list.forEach((item: any, index: number) => {
            if (item[this.idKey] === data[this.idKey]) {
                if (!utils.empty(item.children)) {
                    data.children = utils.CopyNewObject(item.children);
                }
                list.splice(index, 1, data);
                return;
            } else {
                if (!utils.empty(item.children) && item.children.length > 0) {
                    this.edit(item.children, data);
                }
            }
        });
    }
    /**
     * 获取指定id数据
     */
    public get(list: any[], id: string): any {
        if (utils.empty(list)) {
            return null;
        }
        let resData: any = null;
        for (const item of list) {
            if (item[this.idKey] === id) {
                resData = utils.CopyNewObject(item);
                break;
            } else {
                if (!utils.empty(item.children) && item.children.length > 0) {
                    const temData = this.get(item.children, id);
                    if (!utils.empty(temData)) {
                        resData = temData;
                    }
                }
            }
        }
        return resData;
    }
}
